import java.util.*;

public class Main {
    private static String[][] board = new String[9][10]; // 棋盘，9列10行
    private static Map<String, Position> piecePositions = new HashMap<>(); // 棋子位置映射
    private static boolean isRed = true; // AI始终为红方

    // 棋子价值表
    private static final Map<String, Integer> pieceValues = new HashMap<>();
    static {
        pieceValues.put("k", 10000); pieceValues.put("K", 10000); // 红王和黑王
        pieceValues.put("r1", 500); pieceValues.put("r2", 500); // 红车
        pieceValues.put("R1", 500); pieceValues.put("R2", 500); // 黑车
        pieceValues.put("n2", 300); pieceValues.put("N2", 300); // 红马和黑马
        pieceValues.put("c1", 300); pieceValues.put("c2", 300); // 红炮
        pieceValues.put("C1", 300); pieceValues.put("C2", 300); // 黑炮
        pieceValues.put("b1", 200); pieceValues.put("b2", 200); // 红象
        pieceValues.put("B1", 200); pieceValues.put("B2", 200); // 黑象
        pieceValues.put("a1", 200); pieceValues.put("a2", 200); // 红士
        pieceValues.put("A1", 200); pieceValues.put("A2", 200); // 黑士
        pieceValues.put("p1", 100); pieceValues.put("p2", 100); pieceValues.put("p3", 100); // 红兵
        pieceValues.put("p4", 100); pieceValues.put("p5", 100);
        pieceValues.put("P1", 100); pieceValues.put("P2", 100); pieceValues.put("P3", 100); // 黑卒
        pieceValues.put("P4", 100); pieceValues.put("P5", 100);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        initializeBoard(); // 初始化棋盘

        String first = scanner.nextLine(); // 读取第一行输入
        if (first.equals("START")) {
            // AI作为红方先手
            Move m = getBestMove(isRed, 4); // 搜索深度为4
            if (m != null) {
                System.out.println(m);
                System.out.flush();
                updateBoard(m.pieceName, m.toX, m.toY);
                // 检查黑王是否被捕获
                if (piecePositions.get("K") == null) {
                    System.out.println("红方胜出，黑王被捕获！");
                    System.exit(0);
                }
            }
        } else {
            // 对手（黑方）先手，处理黑方移动并响应
            if (!validateInput(first)) {
                System.err.println("无效的首行输入: " + first);
                System.exit(1);
            }
            processMove(first); // 处理黑方移动
            // 检查红王是否被捕获
            if (piecePositions.get("k") == null) {
                System.out.println("黑方胜出，红王被捕获！");
                System.exit(0);
            }
            Move m = getBestMove(isRed, 4); // 生成红方移动
            if (m != null) {
                System.out.println(m);
                System.out.flush();
                updateBoard(m.pieceName, m.toX, m.toY);
                // 检查黑王是否被捕获
                if (piecePositions.get("K") == null) {
                    System.out.println("红方胜出，黑王被捕获！");
                    System.exit(0);
                }
            }
        }

        // 游戏循环
        while (true) {
            String in = scanner.nextLine();
            if (!validateInput(in)) {
                System.err.println("走法输入无效: " + in);
                continue;
            }
            processMove(in); // 处理对手的黑方走法
            // 检查红王是否被捕获
            if (piecePositions.get("k") == null) {
                System.out.println("黑方胜出，红王被捕获！");
                System.exit(0);
            }
            // 检查红方是否被将死
            if (isCheckmated(isRed)) {
                System.out.println("黑方胜出，将死！");
                System.exit(0);
            }
            Move m = getBestMove(isRed, 4); // 生成红方走法
            if (m != null) {
                System.out.println(m);
                System.out.flush();
                updateBoard(m.pieceName, m.toX, m.toY);
                // 检查黑王是否被捕获
                if (piecePositions.get("K") == null) {
                    System.out.println("红方胜出，黑王被捕获！");
                    System.exit(0);
                }
                // 检查黑方是否被将死
                if (isCheckmated(!isRed)) {
                    System.out.println("红方胜出，将死！");
                    System.exit(0);
                }
            }
        }
    }

    // 验证输入是否合法
    private static boolean validateInput(String input) {
        if (input == null || input.trim().isEmpty()) return false;
        String[] parts = input.trim().split("\\s+");
        if (parts.length != 3) return false;

        String name = parts[0];
        try {
            int x = Integer.parseInt(parts[1]);
            int y = Integer.parseInt(parts[2]);
            if (x < 0 || x > 8 || y < 0 || y > 9) return false;
            if (!piecePositions.containsKey(name)) return false;

            Position pos = piecePositions.get(name);
            if (pos != null && !isValidMove(name, pos.x, pos.y, x, y)) {
                System.err.println(name + " 从 (" + pos.x + "," + pos.y + ") 到 (" + x + "," + y + ") 无效");
                return false;
            }
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    // 处理移动
    private static void processMove(String input) {
        String[] parts = input.split("\\s+");
        String name = parts[0];
        int x = Integer.parseInt(parts[1]);
        int y = Integer.parseInt(parts[2]);
        updateBoard(name, x, y);
    }

    // 初始化棋盘
    private static void initializeBoard() {
        for (int i = 0; i < 9; i++)
            for (int j = 0; j < 10; j++)
                board[i][j] = "."; // 用"."表示空位

        // 红方初始位置
        board[4][0] = "k"; piecePositions.put("k", new Position(4, 0));
        board[3][0] = "a1"; piecePositions.put("a1", new Position(3, 0));
        board[5][0] = "a2"; piecePositions.put("a2", new Position(5, 0));
        board[2][0] = "b1"; piecePositions.put("b1", new Position(2, 0));
        board[6][0] = "b2"; piecePositions.put("b2", new Position(6, 0));
        board[7][0] = "n2"; piecePositions.put("n2", new Position(7, 0));
        board[0][0] = "r1"; piecePositions.put("r1", new Position(0, 0));
        board[8][0] = "r2"; piecePositions.put("r2", new Position(8, 0));
        board[1][2] = "c1"; piecePositions.put("c1", new Position(1, 2));
        board[7][2] = "c2"; piecePositions.put("c2", new Position(7, 2));
        board[0][3] = "p1"; piecePositions.put("p1", new Position(0, 3));
        board[2][3] = "p2"; piecePositions.put("p2", new Position(2, 3));
        board[4][3] = "p3"; piecePositions.put("p3", new Position(4, 3));
        board[6][3] = "p4"; piecePositions.put("p4", new Position(6, 3));
        board[8][3] = "p5"; piecePositions.put("p5", new Position(8, 3));

        // 根据红方位置对称设置黑方位置
        Map<String, Position> redMap = new HashMap<>(piecePositions);
        for (Map.Entry<String, Position> e : redMap.entrySet()) {
            String redName = e.getKey();
            Position rp = e.getValue();
            if (!redName.equals(redName.toLowerCase())) continue;

            int bx = 8 - rp.x;
            int by = 9 - rp.y;
            String blackName = redName.toUpperCase();
            board[bx][by] = blackName;
            piecePositions.put(blackName, new Position(bx, by));
        }
    }

    // 更新棋盘状态
    private static void updateBoard(String name, int x, int y) {
        Position old = piecePositions.get(name);
        if (old != null) {
            board[old.x][old.y] = ".";
            if (!board[x][y].equals(".")) {
                String cap = getPieceNameAt(x, y);
                piecePositions.remove(cap); // 移除被吃掉的棋子
            }
            board[x][y] = name;
            piecePositions.put(name, new Position(x, y));
            if (!board[x][y].equals(name)) {
                System.err.println("棋盘状态不一致于 (" + x + "," + y + ")");
                System.exit(1);
            }
        }
    }

    // 获取最佳移动
    private static Move getBestMove(boolean redTurn, int depth) {
        List<Move> allMoves = new ArrayList<>();
        for (String name : piecePositions.keySet()) {
            boolean isPieceRed = Character.isLowerCase(name.charAt(0));
            if (isPieceRed == redTurn) {
                Position p = piecePositions.get(name);
                allMoves.addAll(generateMovesForPiece(name, p.x, p.y, redTurn));
            }
        }
        if (allMoves.isEmpty()) return null;

        Move bestMove = null;
        int bestValue = redTurn ? Integer.MIN_VALUE : Integer.MAX_VALUE;
        for (Move m : allMoves) {
            String[][] tempBoard = new String[9][10];
            Map<String, Position> tempPositions = new HashMap<>(piecePositions);
            for (int i = 0; i < 9; i++) tempBoard[i] = board[i].clone();
            String name = m.pieceName;
            Position oldPos = tempPositions.get(name);
            tempBoard[oldPos.x][oldPos.y] = ".";
            if (!tempBoard[m.toX][m.toY].equals(".")) {
                String cap = getPieceNameAt(m.toX, m.toY);
                tempPositions.remove(cap);
            }
            tempBoard[m.toX][m.toY] = name;
            tempPositions.put(name, new Position(m.toX, m.toY));
            String[][] originalBoard = board;
            Map<String, Position> originalPositions = piecePositions;
            board = tempBoard;
            piecePositions = tempPositions;
            int value = alphaBeta(!redTurn, depth - 1, Integer.MIN_VALUE, Integer.MAX_VALUE);
            board = originalBoard;
            piecePositions = originalPositions;
            if (redTurn && value > bestValue) {
                bestValue = value;
                bestMove = m;
            } else if (!redTurn && value < bestValue) {
                bestValue = value;
                bestMove = m;
            }
        }
        return bestMove;
    }

    // Alpha-Beta剪枝算法
    private static int alphaBeta(boolean redTurn, int depth, int alpha, int beta) {
        if (depth == 0 || isGameOver()) {
            return evaluateBoard(redTurn);
        }
        List<Move> allMoves = new ArrayList<>();
        for (String name : piecePositions.keySet()) {
            boolean isPieceRed = Character.isLowerCase(name.charAt(0));
            if (isPieceRed == redTurn) {
                Position p = piecePositions.get(name);
                allMoves.addAll(generateMovesForPiece(name, p.x, p.y, redTurn));
            }
        }
        if (allMoves.isEmpty()) return evaluateBoard(redTurn);

        // 移动排序：优先考虑防御性移动和吃子
        allMoves.sort((m1, m2) -> {
            boolean isDefensive1 = isDefensiveMove(m1, redTurn);
            boolean isDefensive2 = isDefensiveMove(m2, redTurn);
            boolean capture1 = !board[m1.toX][m1.toY].equals(".");
            boolean capture2 = !board[m2.toX][m2.toY].equals(".");
            if (isDefensive1 && !isDefensive2) return -1;
            if (!isDefensive1 && isDefensive2) return 1;
            if (capture1 && capture2) {
                int value1 = pieceValues.getOrDefault(getPieceNameAt(m1.toX, m1.toY), 0);
                int value2 = pieceValues.getOrDefault(getPieceNameAt(m2.toX, m2.toY), 0);
                return Integer.compare(value2, value1); // 按吃子价值排序
            }
            return Boolean.compare(capture2, capture1); // 吃子优先
        });

        if (redTurn) {
            int maxEval = Integer.MIN_VALUE;
            for (Move m : allMoves) {
                String[][] tempBoard = new String[9][10];
                Map<String, Position> tempPositions = new HashMap<>(piecePositions);
                for (int i = 0; i < 9; i++) tempBoard[i] = board[i].clone();
                String name = m.pieceName;
                Position oldPos = tempPositions.get(name);
                tempBoard[oldPos.x][oldPos.y] = ".";
                if (!tempBoard[m.toX][m.toY].equals(".")) {
                    String cap = getPieceNameAt(m.toX, m.toY);
                    tempPositions.remove(cap);
                }
                tempBoard[m.toX][m.toY] = name;
                tempPositions.put(name, new Position(m.toX, m.toY));
                String[][] originalBoard = board;
                Map<String, Position> originalPositions = piecePositions;
                board = tempBoard;
                piecePositions = tempPositions;
                int eval = alphaBeta(!redTurn, depth - 1, alpha, beta);
                board = originalBoard;
                piecePositions = originalPositions;
                maxEval = Math.max(maxEval, eval);
                alpha = Math.max(alpha, eval);
                if (beta <= alpha) break;
            }
            return maxEval;
        } else {
            int minEval = Integer.MAX_VALUE;
            for (Move m : allMoves) {
                String[][] tempBoard = new String[9][10];
                Map<String, Position> tempPositions = new HashMap<>(piecePositions);
                for (int i = 0; i < 9; i++) tempBoard[i] = board[i].clone();
                String name = m.pieceName;
                Position oldPos = tempPositions.get(name);
                tempBoard[oldPos.x][oldPos.y] = ".";
                if (!tempBoard[m.toX][m.toY].equals(".")) {
                    String cap = getPieceNameAt(m.toX, m.toY);
                    tempPositions.remove(cap);
                }
                tempBoard[m.toX][m.toY] = name;
                tempPositions.put(name, new Position(m.toX, m.toY));
                String[][] originalBoard = board;
                Map<String, Position> originalPositions = piecePositions;
                board = tempBoard;
                piecePositions = tempPositions;
                int eval = alphaBeta(!redTurn, depth - 1, alpha, beta);
                board = originalBoard;
                piecePositions = originalPositions;
                minEval = Math.min(minEval, eval);
                beta = Math.min(beta, eval);
                if (beta <= alpha) break;
            }
            return minEval;
        }
    }

    private static int evaluateBoard(boolean redTurn) {
        int score = 0;
        for (String name : piecePositions.keySet()) {
            boolean isPieceRed = Character.isLowerCase(name.charAt(0));
            int value = pieceValues.getOrDefault(name, 0);
            if (isPieceRed == redTurn) {
                score += value;
            } else {
                score -= value;
            }
        }
        Position kingPos = getKingPosition(redTurn);
        if (kingPos == null) {
            return redTurn ? Integer.MIN_VALUE + 1000 : Integer.MAX_VALUE - 1000;
        }
        if (isUnderAttack(kingPos.x, kingPos.y, redTurn)) {
            score -= 10000; // 大幅增加王受威胁的惩罚
        }
        if (isCheckmated(redTurn)) {
            return redTurn ? Integer.MIN_VALUE + 1000 : Integer.MAX_VALUE - 1000;
        }
        return score;
    }

    // 检查游戏是否结束
    private static boolean isGameOver() {
        return piecePositions.get("k") == null || piecePositions.get("K") == null;
    }

    // 为特定棋子生成合法移动
    private static List<Move> generateMovesForPiece(String name, int x, int y, boolean isRed) {
        List<Move> moves = new ArrayList<>();
        boolean isPieceRed = Character.isLowerCase(name.charAt(0));
        if (isPieceRed != isRed) return moves;

        if (name.equals("k") || name.equals("K")) {
            moves.addAll(generateKingMoves(name, x, y, isRed));
        } else if (name.startsWith("a") || name.startsWith("A")) {
            moves.addAll(generateAdvisorMoves(name, x, y, isRed));
        } else if (name.startsWith("b") || name.startsWith("B")) {
            moves.addAll(generateBishopMoves(name, x, y, isRed));
        } else if (name.equals("n2") || name.equals("N2")) {
            moves.addAll(generateKnightMoves(name, x, y, isRed));
        } else if (name.startsWith("r") || name.startsWith("R")) {
            moves.addAll(generateRookMoves(name, x, y, isRed));
        } else if (name.startsWith("c") || name.startsWith("C")) {
            moves.addAll(generateCannonMoves(name, x, y, isRed));
        } else if (name.startsWith("p") || name.startsWith("P")) {
            moves.addAll(generatePawnMoves(name, x, y, isRed));
        }
        return moves;
    }

    // 生成王的移动
    private static List<Move> generateKingMoves(String name, int x, int y, boolean isRed) {
        List<Move> moves = new ArrayList<>();
        int[][] directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
        for (int[] dir : directions) {
            int nx = x + dir[0];
            int ny = y + dir[1];
            if (isRed ? (nx >= 3 && nx <= 5 && ny >= 0 && ny <= 2) : (nx >= 3 && nx <= 5 && ny >= 7 && ny <= 9)) {
                if (board[nx][ny] == "." || isEnemy(board[nx][ny], isRed)) {
                    moves.add(new Move(name, nx, ny));
                }
            }
        }
        return moves;
    }

    // 生成士的移动
    private static List<Move> generateAdvisorMoves(String name, int x, int y, boolean isRed) {
        List<Move> moves = new ArrayList<>();
        int[][] directions = {{1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
        for (int[] dir : directions) {
            int nx = x + dir[0];
            int ny = y + dir[1];
            if (isRed ? (nx >= 3 && nx <= 5 && ny >= 0 && ny <= 2) : (nx >= 3 && nx <= 5 && ny >= 7 && ny <= 9)) {
                if (board[nx][ny] == "." || isEnemy(board[nx][ny], isRed)) {
                    moves.add(new Move(name, nx, ny));
                }
            }
        }
        return moves;
    }

    // 生成象的移动
    private static List<Move> generateBishopMoves(String name, int x, int y, boolean isRed) {
        List<Move> moves = new ArrayList<>();
        int[][] directions = {{2, 2}, {2, -2}, {-2, 2}, {-2, -2}};
        for (int[] dir : directions) {
            int nx = x + dir[0];
            int ny = y + dir[1];
            int mx = x + dir[0] / 2;
            int my = y + dir[1] / 2;
            if (mx < 0 || mx > 8 || my < 0 || my > 9) continue;
            if (isRed ? (ny >= 0 && ny <= 4) : (ny >= 5 && ny <= 9)) {
                if (nx >= 0 && nx <= 8 && board[mx][my] == "." && (board[nx][ny] == "." || isEnemy(board[nx][ny], isRed))) {
                    moves.add(new Move(name, nx, ny));
                }
            }
        }
        return moves;
    }

    // 生成马的移动
    private static List<Move> generateKnightMoves(String name, int x, int y, boolean isRed) {
        List<Move> moves = new ArrayList<>();
        int[][] directions = {{1, 2}, {1, -2}, {-1, 2}, {-1, -2}, {2, 1}, {2, -1}, {-2, 1}, {-2, -1}};
        int[][] blocks = {{0, 1}, {0, -1}, {0, 1}, {0, -1}, {1, 0}, {1, 0}, {-1, 0}, {-1, 0}};
        for (int i = 0; i < directions.length; i++) {
            int nx = x + directions[i][0];
            int ny = y + directions[i][1];
            int bx = x + blocks[i][0];
            int by = y + blocks[i][1];
            if (bx < 0 || bx > 8 || by < 0 || by > 9) continue;
            if (nx >= 0 && nx <= 8 && ny >= 0 && ny <= 9 && board[bx][by] == ".") {
                if (board[nx][ny] == "." || isEnemy(board[nx][ny], isRed)) {
                    moves.add(new Move(name, nx, ny));
                }
            }
        }
        return moves;
    }

    // 生成车的移动
    private static List<Move> generateRookMoves(String name, int x, int y, boolean isRed) {
        List<Move> moves = new ArrayList<>();
        for (int nx = x + 1; nx < 9; nx++) {
            if (board[nx][y] == ".") {
                moves.add(new Move(name, nx, y));
            } else {
                if (isEnemy(board[nx][y], isRed)) {
                    moves.add(new Move(name, nx, y));
                }
                break;
            }
        }
        for (int nx = x - 1; nx >= 0; nx--) {
            if (board[nx][y] == ".") {
                moves.add(new Move(name, nx, y));
            } else {
                if (isEnemy(board[nx][y], isRed)) {
                    moves.add(new Move(name, nx, y));
                }
                break;
            }
        }
        for (int ny = y + 1; ny < 10; ny++) {
            if (board[x][ny] == ".") {
                moves.add(new Move(name, x, ny));
            } else {
                if (isEnemy(board[x][ny], isRed)) {
                    moves.add(new Move(name, x, ny));
                }
                break;
            }
        }
        for (int ny = y - 1; ny >= 0; ny--) {
            if (board[x][ny] == ".") {
                moves.add(new Move(name, x, ny));
            } else {
                if (isEnemy(board[x][ny], isRed)) {
                    moves.add(new Move(name, x, ny));
                }
                break;
            }
        }
        return moves;
    }

    // 生成炮的移动
    private static List<Move> generateCannonMoves(String name, int x, int y, boolean isRed) {
        List<Move> moves = new ArrayList<>();

        // 右水平方向
        boolean foundPiece = false;
        for (int nx = x + 1; nx < 9; nx++) {
            if (board[nx][y] == ".") {
                if (!foundPiece) {
                    moves.add(new Move(name, nx, y));
                }
            } else {
                if (!foundPiece) {
                    foundPiece = true;
                } else {
                    if (isEnemy(board[nx][y], isRed)) {
                        moves.add(new Move(name, nx, y));
                    }
                    break;
                }
            }
        }

        // 左水平方向
        foundPiece = false;
        for (int nx = x - 1; nx >= 0; nx--) {
            if (board[nx][y] == ".") {
                if (!foundPiece) {
                    moves.add(new Move(name, nx, y));
                }
            } else {
                if (!foundPiece) {
                    foundPiece = true;
                } else {
                    if (isEnemy(board[nx][y], isRed)) {
                        moves.add(new Move(name, nx, y));
                    }
                    break;
                }
            }
        }

        // 上垂直方向
        foundPiece = false;
        for (int ny = y + 1; ny < 10; ny++) {
            if (board[x][ny] == ".") {
                if (!foundPiece) {
                    moves.add(new Move(name, x, ny));
                }
            } else {
                if (!foundPiece) {
                    foundPiece = true;
                } else {
                    if (isEnemy(board[x][ny], isRed)) {
                        moves.add(new Move(name, x, ny));
                    }
                    break;
                }
            }
        }

        // 下垂直方向
        foundPiece = false;
        for (int ny = y - 1; ny >= 0; ny--) {
            if (board[x][ny] == ".") {
                if (!foundPiece) {
                    moves.add(new Move(name, x, ny));
                }
            } else {
                if (!foundPiece) {
                    foundPiece = true;
                } else {
                    if (isEnemy(board[x][ny], isRed)) {
                        moves.add(new Move(name, x, ny));
                    }
                    break;
                }
            }
        }

        return moves;
    }

    // 生成兵/卒的移动
    private static List<Move> generatePawnMoves(String name, int x, int y, boolean isRed) {
        List<Move> moves = new ArrayList<>();
        int ny = isRed ? y + 1 : y - 1;
        if (ny >= 0 && ny <= 9 && (board[x][ny] == "." || isEnemy(board[x][ny], isRed))) {
            moves.add(new Move(name, x, ny));
        }
        if (isRed ? y >= 5 : y <= 4) { // 过河后可左右移动
            if (x > 0 && (board[x - 1][y] == "." || isEnemy(board[x - 1][y], isRed))) {
                moves.add(new Move(name, x - 1, y));
            }
            if (x < 8 && (board[x + 1][y] == "." || isEnemy(board[x + 1][y], isRed))) {
                moves.add(new Move(name, x + 1, y));
            }
        }
        return moves;
    }

    // 检查移动是否合法
    private static boolean isValidMove(String name, int fromX, int fromY, int toX, int toY) {
        Position pos = piecePositions.get(name);
        if (pos == null || pos.x != fromX || pos.y != fromY) return false;
        boolean isPieceRed = Character.isLowerCase(name.charAt(0));
        List<Move> legalMoves = generateMovesForPiece(name, fromX, fromY, isPieceRed);
        for (Move move : legalMoves) {
            if (move.toX == toX && move.toY == toY) return true;
        }
        return false;
    }

    // 判断是否为敌方棋子
    private static boolean isEnemy(String piece, boolean isRed) {
        if (piece == ".") return false;
        return (isRed && Character.isUpperCase(piece.charAt(0))) || (!isRed && Character.isLowerCase(piece.charAt(0)));
    }

    // 获取指定位置的棋子名称
    private static String getPieceNameAt(int x, int y) {
        for (Map.Entry<String, Position> entry : piecePositions.entrySet()) {
            Position p = entry.getValue();
            if (p.x == x && p.y == y) {
                return entry.getKey();
            }
        }
        return null;
    }

    // 获取王的当前位置
    private static Position getKingPosition(boolean isRedTurn) {
        String kingName = isRedTurn ? "k" : "K";
        return piecePositions.get(kingName);
    }

    // 检查位置是否受攻击
    private static boolean isUnderAttack(int x, int y, boolean isRed) {
        for (String enemyName : piecePositions.keySet()) {
            boolean isEnemyRed = Character.isLowerCase(enemyName.charAt(0));
            if (isEnemyRed != isRed) {
                Position p = piecePositions.get(enemyName);
                List<Move> enemyMoves = generateMovesForPiece(enemyName, p.x, p.y, isEnemyRed);
                for (Move m : enemyMoves) {
                    if (m.toX == x && m.toY == y) return true;
                }
            }
        }
        return false;
    }

    // 检查是否被将死
    private static boolean isCheckmated(boolean isRed) {
        String kingName = isRed ? "k" : "K";
        Position kingPos = piecePositions.get(kingName);
        if (kingPos == null) return false;
        if (!isUnderAttack(kingPos.x, kingPos.y, isRed)) return false;

        for (String name : piecePositions.keySet()) {
            if (Character.isLowerCase(name.charAt(0)) == isRed) {
                Position p = piecePositions.get(name);
                List<Move> moves = generateMovesForPiece(name, p.x, p.y, isRed);
                for (Move m : moves) {
                    String[][] tempBoard = new String[9][10];
                    Map<String, Position> tempPositions = new HashMap<>(piecePositions);
                    for (int i = 0; i < 9; i++) tempBoard[i] = board[i].clone();
                    tempBoard[p.x][p.y] = ".";
                    if (!tempBoard[m.toX][m.toY].equals(".")) tempPositions.remove(getPieceNameAt(m.toX, m.toY));
                    tempBoard[m.toX][m.toY] = name;
                    tempPositions.put(name, new Position(m.toX, m.toY));
                    String[][] originalBoard = board;
                    Map<String, Position> originalPositions = piecePositions;
                    board = tempBoard;
                    piecePositions = tempPositions;
                    boolean stillInCheck = isUnderAttack(kingPos.x, kingPos.y, isRed);
                    board = originalBoard;
                    piecePositions = originalPositions;
                    if (!stillInCheck) return false;
                }
            }
        }
        return true;
    }

    // 判断是否为防御性移动
    private static boolean isDefensiveMove(Move m, boolean redTurn) {
        Position kingPos = getKingPosition(redTurn);
        if (kingPos == null) return false;
        int dx = Math.abs(m.toX - kingPos.x);
        int dy = Math.abs(m.toY - kingPos.y);
        return dx <= 1 && dy <= 1; // 靠近王的移动视为防御性
    }
}